﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGKata
{
    class Character
    {
        public string name { get; set; }
        public int strength { get; set; }
        public int dexterity { get; set; }
        public int tempDexterity { get; set; }
        public int constitution { get; set; }
        public Weapon weapon { get; set; }
        public Armor armor { get; set; }
        public Character opponent { get; set; }
        public int rollDexterity { get; set; }
        private bool die = false;


        public Character()
        {

        }

        public Character(string n, int s, int d, int c)
        {
            name = n;
            strength = s;
            dexterity = d;
            constitution = c;
        }

        public void setStrength(string num)
        {
            while (!Roll.isValidInt(num))
            {
                Console.WriteLine("That's not a number between 0 and 20! Please choose again.");
                num = Console.ReadLine();
            }
            int number = int.Parse(num);
            strength = number;
        }

        public void setDexterity(string num)
        {
            while (!Roll.isValidInt(num))
            {
                Console.WriteLine("That's not a number between 0 and 20! Please choose again.");
                num = Console.ReadLine();
            }
            int number = int.Parse(num);
            dexterity = number;
        }

        public void changeDexterity()
        {
            if (armor.encumbrance > 0)
            {
                tempDexterity = dexterity - armor.encumbrance;
            }
        }

        public void resetTempDexterity()
        {
            tempDexterity = dexterity;
        }

        public void setConstitution(string num)
        {
            while (!Roll.isValidInt(num))
            {
                Console.WriteLine("That's not a number between 0 and 20! Please choose again.");
                num = Console.ReadLine();
            }
            int number = int.Parse(num);
            constitution = number;
        }

        public bool hasOpponent()
        {
            return opponent != null;
        }

        public bool isWeaponEquipped()
        {
            return weapon != null;
        }
        public bool isArmorEquipped()
        {
            return armor != null;
        }

        public void setWeapon(Weapon w)
        {
            weapon = w;
        }

        public Weapon getWeapon()
        {
            return weapon;
        }
        public void setArmor(Armor a)
        {
            armor = a;
            resetTempDexterity();
            changeDexterity();
        }


        public void Attack()
        {
            if (Roll.rolls(opponent.tempDexterity) - Roll.rolls(this.tempDexterity) > 0)
            {
                int value = attackDamage() - opponent.armor.protection;
                if (value > 0)
                {
                    Console.WriteLine("{0} attacked {1} with {2} damage", this.name, opponent.name, value);
                    opponent.takeDamage(value);
                }
            }
        }

        public int attackDamage()
        {
            if (isWeaponEquipped())
            {
                return Roll.rolls(this.weapon.damage) + Roll.rolls(getBaseAttribute());
            }
            else
            {
                return Roll.rolls(0) + Roll.rolls(strength);
            }
        }

        public void setRollDexterity()
        {
            rollDexterity = Roll.rolls(tempDexterity);
        }

        public int getBaseAttribute()
        {
            switch (this.weapon.baseAttribute)
            {
                case "strength":
                    return strength;
                case "dexterity":
                    return dexterity;
                default:
                    return 0;
            }
        }
        public void takeDamage(int damage)
        {
            constitution = constitution - damage;
            if (constitution < 1)
            {
                die = true;
            }
        }

        public bool isDead()
        {
            return die;
        }       
    }
}
